package com.cc.micro.dataService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.micro.api.model.LoanInfo;
import com.cc.micro.api.service.LoanInfoService;
import com.cc.micro.api.vo.MultiProduct;
import com.cc.micro.common.constants.YLBConstant;
import com.cc.micro.common.enumeration.HttpCode;
import com.cc.micro.dataService.mapper.LoanInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * <p>
 * 产品信息表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2023-07-01
 */
@DubboService(interfaceClass = LoanInfoService.class,version = "1.0")
public class LoanInfoServiceImpl extends ServiceImpl<LoanInfoMapper, LoanInfo> implements LoanInfoService {

    @Override
    public MultiProduct queryIndexPageProduct() {
        Page<LoanInfo> loanInfoPage = new Page<>(1, 10);
        IPage<LoanInfo> newbieTreasure = this.pageQueryByProductType(loanInfoPage, YLBConstant.NEWBIE_TREASURE);
        IPage<LoanInfo> optimization = this.pageQueryByProductType(loanInfoPage, YLBConstant.OPTIMIZATION);
        IPage<LoanInfo> looseLabels = this.pageQueryByProductType(loanInfoPage, YLBConstant.LOOSE_LABELS);
        MultiProduct multiProduct = new MultiProduct();
        multiProduct.setNewbieTreasure(newbieTreasure.getRecords());
        multiProduct.setOptimization(optimization.getRecords());
        multiProduct.setLooseLabels(looseLabels.getRecords());
        return multiProduct;
    }

    @Override
    public IPage<LoanInfo> pageQueryByProductType(Page<LoanInfo> page, Integer type) {
        long current = page.getCurrent();
        long size = page.getSize();
        if (current<1) {
            page.setCurrent(1L);
        }
        if (size<0) {
            page.setSize(10);
        }
        if (type == null||type<YLBConstant.NEWBIE_TREASURE ||type>YLBConstant.LOOSE_LABELS) {
            throw new RuntimeException(HttpCode.NO_SUPPORT_PRODUCT_TYPE.getMsg());
        }
        IPage<LoanInfo> result = baseMapper.selectPage(page,
                new LambdaQueryWrapper<LoanInfo>().
                        eq(LoanInfo::getProductType, type)
                        .orderByDesc(LoanInfo::getReleaseTime));
        return result;
    }

}
